/**
 * Created by fuhao on 15/9/6.
 */
/**
 * Created by fuhao on 15/9/2.
 */
function UserContractPayment() {
    this.method, this.action, this.operationMethod, this.operationMethod_2;
}

UserContractPayment.prototype.load = function () {
    var objThis = this;
    var params = {};
    params.contractId = contractId;
    $('#datagrid_UserContractPayment').datagrid({
        url: basePath + '/' + this.action + '/' + objThis.method,
        fitColumns: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        pageSize: 50,
        pageNumber: 1,
        pageList: [20, 50, 100],
        loadMsg: '数据正在加载中...',
        queryParams: params,
        singleSelect: true,
        selectOnCheck: false,
        checkOnSelect: false,
        columns: [[
            {
                field: 'id',
                title: '<span class="txtcenter">房租账单记录ID</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'projectName',
                title: '<span class="txtcenter">项目名称</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'projectDesc',
                title: '<span class="txtcenter">项目描述</span>',
                resizable: true,
                hidden: false,
                sortable: true,
                width: '200',
                align: 'center'
            },
            {
                field: 'rentPrice',
                title: '<span class="txtcenter">房租金额</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'payCode',
                title: '<span class="txtcenter">支付流水号</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'payStatus',
                title: '<span class="txtcenter">状态</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'createTime',
                title: '<span class="txtcenter">创建日期</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center',
                formatter: function (value, row, index) {
                    var dd = new Date(value);
                    return formateDate(dd);
                }
            },
            {
                field: 'action',
                title: '<span class="txtcenter">操作</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center',
                formatter: function (value, row, index) {
                    var html = "";
                    if (row.action == 1) {
                        html += '<a class="btn btn-sm green" href="' + basePath + '/' + objThis.action + '/' + objThis.operationMethod_2 + '/' + row.id + '">作废</a>';
                    }
                    html += '<a class="btn btn-sm green" href="' + basePath + '/' + objThis.action + '/' + objThis.operationMethod + '/' + row.id + '">详情</a>';
                    return html;
                }
            }
        ]],
        onLoadSuccess: function (data) {
            validateReqStatus(data);
        },
        onLoadError: function (data) {
            validateReqStatus(data);
        }
    });
};

UserContractPayment.prototype.add = function () {
    var objThis = this;
    $('#addRentBillHistory').one('click', function () {
            $.ajax({
                type: 'get',
                url: basePath + '/' + objThis.action + '/add',
                success: function (data) {
                    var $dialog = $('#addRentBillHistory_div');
                    $dialog.html(data);
                    $dialog.dialog({
                        title: '新增账单',
                        width: 400,
                        height: 217,
                        closed: false,
                        modal: true,
                        buttons: [{
                            text: '保存',
                            iconCls: 'icon-ok',
                            handler: function () {
                                objThis.submit();
                            }
                        }]
                    });
                    $('#totalSum').numberbox({
                        min: 1.00,
                        max: 99999.00,
                        precision: 2,
                        size:8,
                        maxlength:8
                    });
                    $('#totalSum').next().find('input:eq(0)').attr('placeholder', '请输入金额1.00-99999.00').css({'padding-left':'10px'});
                    objThis.add();
                },
                error: function () {
                    objThis.add();
                }
            });
        }
    );
};

UserContractPayment.prototype.submit = function () {
    var objThis = this;
    var params = $('#inputForm_RentBill').serializeJson();
    params["contractId"] = contractId;
    $.ajax({
        type: 'post',
        url: basePath + '/' + objThis.action + '/insert',
        data: params,
        beforeSend: function () {
            var type = $('#inputForm_RentBill').find("input:checked");
            if (type.attr('id') == 'other' && isEmpty($('#other_input').val())) {
                $.messager.alert('提示', '请输入项目类型');
                return false;
            }
            var $this;
            try {
                $('#inputForm_RentBill').find('.input').each(function (i) {
                    $this = $(this);
                    if (isEmpty($this.val())) {
                        throw $this.attr('placeholder');
                    }
                });
            } catch (e) {
                $.messager.alert('提示', e);
                return false;
            }
            return true;
        },
        success: function (data) {
            validateReqStatus(data, function () {
                objThis.load();
            });
        },
        error: function () {

        }
    });
};

UserContractPayment.prototype.init = function () {
    var objThis = this;
    objThis.load();
    objThis.add();
};

function UserContractFollowUp() {
    this.method, this.action, this.operationMethod;
}

UserContractFollowUp.prototype.load = function (params) {
    var objThis = this;
    var params = {};
    params.contractId = contractId;
    $('#datagrid_UserContractFollowUp').datagrid({
        url: basePath + '/' + this.action + '/' + objThis.method,
        fitColumns: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        pageSize: 50,
        pageNumber: 1,
        pageList: [20, 50, 100],
        loadMsg: '数据正在加载中...',
        queryParams: params,
        singleSelect: true,
        selectOnCheck: false,
        checkOnSelect: false,
        columns: [[
            {
                field: 'createTime',
                title: '<span class="txtcenter">时间</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'typeName',
                title: '<span class="txtcenter">操作类型</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'operatorUserName',
                title: '<span class="txtcenter">操作员</span>',
                resizable: true,
                hidden: false,
                sortable: true,
                width: '200',
                align: 'center'
            },
            {
                field: 'mobile',
                title: '<span class="txtcenter">手机</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            },
            {
                field: 'memo',
                title: '<span class="txtcenter">备注/操作内容</span>',
                resizable: true,
                hidden: false,
                sortable: false,
                width: '200',
                align: 'center'
            }
        ]],
        onLoadSuccess: function (data) {
            validateReqStatus(data);
        },
        onLoadError: function (data) {
            validateReqStatus(data);
        }
    });
};

UserContractFollowUp.prototype.add = function () {
    var objThis = this;
    $('#followUpInputBtn').one('click', function () {
        objThis.submit();
    });
};

UserContractFollowUp.prototype.submit = function () {
    var objThis = this;
    var params = $('#inputForm_followUp').serializeJson();
    params["contractId"] = contractId;
    var memo = $('#inputForm_followUp').find('[name=memo]');
    $.ajax({
        type: 'post',
        url: basePath + '/' + objThis.action + '/insert',
        data: params,
        beforeSend: function () {
            if (isEmpty(memo.val())) {
                $.messager.alert('提示', memo.attr('placeholder'));
                objThis.add();
                return false;
            }
            return true;
        },
        success: function (data) {
            validateReqStatus(data, function () {
                objThis.load();
                memo.val('');
            });
            objThis.add();
        },
        error: function () {
            objThis.add();
        }
    });
};


UserContractFollowUp.prototype.init = function () {
    var objThis = this;
    objThis.load();
    objThis.add();
};

$(document).ready(function () {
    var payment = new UserContractPayment();
    payment.action = "userPayment";
    payment.method = "list";
    payment.operationMethod = "details";
    payment.operationMethod_2 = "closed";
    payment.init();

    var followUp = new UserContractFollowUp();
    followUp.action = "userContractLog";
    followUp.method = "list";
    followUp.init();

    audit();// 审核
    tabToggle();
    termination();
});

function audit() {
    $('#auditBtn').one('click', function () {
        var $this = $(this);
        if ($this.hasClass('btn_submit')) {
            $.ajax({
                async: true,
                type: 'post',
                url: basePath + '/userContract/audit/' + contractId,
                success: function (data) {
                    validateReqStatus(data, function () {
                        $this.removeClass().addClass('btn_cancel');
                        window.location.reload();
                    });
                    audit();
                },
                error: function () {
                    audit();
                }
            });
        } else {
            $.messager.alert('提示', '该租赁合同已经审核通过！');
        }
    });
}
function termination() {
    $('#terminationBtn').one('click', function () {
        var $this = $(this);
        if ($this.hasClass('btn_submit')) {
            $.ajax({
                async: true,
                type: 'post',
                url: basePath + '/userContract/termination/' + contractId,
                success: function (data) {
                    validateReqStatus(data, function () {
                        $this.removeClass().addClass('btn_cancel');
                        window.location.reload();
                    });
                    termination();
                },
                error: function () {
                    termination();
                }
            });
        } else {
            $.messager.alert('提示', '该租赁合同已经终止！');
        }
    });
}

